// Copyright (C) 2010 Razvan Constantin <razvan.constantin@rconst.net>
//
// This file is part of FileBow.
//
// FileBow is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// FileBow is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with FileBow.  If not, see <http://www.gnu.org/licenses/>.

#include "logic/history.h"

#include "logic/file-scorer.h"
#include "utils/npath.h"

using namespace std;

namespace filebow {

const int kHistorySize = 100;
const string kEmpty;

History::History() {
}

History::~History() {
}

const string& History::active() const {
  return mHistory.empty() ? kEmpty : mHistory.back();
}

void History::TrackOpen(const string& file) {
  if (mHistory.size() >= kHistorySize) {
    if (--mFrequency[mHistory.front()] <= 0) {
      mFrequency.erase(mHistory.front());
    }
    mHistory.pop();
  }
  mHistory.push(file);
  ++mFrequency[file];
}

void History::TrackActivate(const string& file) {
  TrackOpen(file);
}

void History::TrackClose(const string& file) {
  // TODO: Handle close.
}

FileScorer* History::GetScorer(PathTree* tree) const {
  DefaultScorer* scorer = new DefaultScorer();
  for (map<string, int>::const_iterator it = mFrequency.begin(); it != mFrequency.end(); ++it) {
    PathTree::Node* node = tree->Find(it->first);
    if (node) {
      scorer->SetScore(node, it->second * 10);
    }
  }
  return scorer;
}

} // namespace filebow
